/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.helwen.restaurantbooking.ui;

import com.helwen.restaurantbooking.bo.IBookingBo;
import com.helwen.restaurantbooking.helper.BeanManagerHelper;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Nghiata
 */
public class HistoryPanel extends JPanel {

    private static final IBookingBo BOOKING_BO = (IBookingBo) BeanManagerHelper.getBean("bookingBo");
    private JPanel searchPanel;
    private JLabel labelName;
    private JLabel labelContact;
    private JTextField txtName;
    private JTextField txtContact;
    private JButton btnSearch;
    private JButton btnRefresh;
    private JButton btnClear;
    private JScrollPane scrollPane;
    private JTable tblHistory;
    private Object[][] dataModel;
    private MainUI parentComponent;
    private static final String[] columnsName = new String[]{"Name", "Contact", "Date", "Time", "Staff", "Area", "Table Number", "Number of pack", "Remarks", "Status"};

    public HistoryPanel() {
        initDataModel();
        initTable();
        initLayout();
    }

    private void initLayout() {
        this.setLayout(new BorderLayout());
        searchPanel = new JPanel();
        searchPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        labelName = new JLabel("Name:");
        txtName = new JTextField();
        txtName.setColumns(25);
        txtContact = new JTextField();
        txtContact.setColumns(25);
        btnSearch = new JButton("Search");
        btnRefresh = new JButton("Refresh");
        btnClear = new JButton("Clear");
        searchPanel.add(labelName);
        searchPanel.add(txtName);
        labelContact = new JLabel("Contact:");
        searchPanel.add(labelContact);
        searchPanel.add(txtContact);
        searchPanel.add(btnSearch);
        searchPanel.add(btnRefresh);
        searchPanel.add(btnClear);
        btnSearch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnSearchClick(e);
            }
        });

        btnRefresh.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnRefreshClick(e);
            }
        });
        btnClear.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                btnClearClick(e);
            }
        });
        scrollPane = new JScrollPane(tblHistory);
        tblHistory.setFillsViewportHeight(true);


        this.add(searchPanel, BorderLayout.NORTH);
        this.add(scrollPane, BorderLayout.CENTER);

    }

    private void initDataModel() {
        dataModel = BOOKING_BO.getDateModel("", "","","");
    }

    private void initTable() {

        tblHistory = new JTable();
        tblHistory.setModel(new DefaultTableModel(dataModel, columnsName));
        tblHistory.getTableHeader().addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int col = tblHistory.columnAtPoint(e.getPoint());
                String name = tblHistory.getColumnName(col);
//                System.out.println("Column index selected " + col + " " + name);
                if(name.equals("Name")){
                     dataModel = BOOKING_BO.getDateModel(txtName.getText()+"", txtContact.getText()+"","customerName","desc");
                     columnsName[0]="Name (ASC)";
                     tblHistory.setModel(new DefaultTableModel(dataModel, columnsName));
                }else if(name.equals("Name (ASC)")){
                    dataModel = BOOKING_BO.getDateModel(txtName.getText()+"", txtContact.getText()+"","customerName","asc");
                    columnsName[0]="Name";
                    tblHistory.setModel(new DefaultTableModel(dataModel, columnsName));
                }else if(name.equals("Contact")){
                     dataModel = BOOKING_BO.getDateModel(txtName.getText()+"", txtContact.getText()+"","contact","desc");
                     columnsName[1]="Contact (ASC)";
                     tblHistory.setModel(new DefaultTableModel(dataModel, columnsName));
                }else if(name.equals("Contact (ASC)")){
                     dataModel = BOOKING_BO.getDateModel(txtName.getText()+"", txtContact.getText()+"","contact","asc");
                      columnsName[1]="Contact";
                     tblHistory.setModel(new DefaultTableModel(dataModel, columnsName));
                }
            }
        });
    }

    private void btnSearchClick(ActionEvent e) {
        dataModel = BOOKING_BO.getDateModel(txtName.getText(), txtContact.getText(),"","");
        tblHistory.setModel(new DefaultTableModel(dataModel, columnsName));
    }

    private void btnRefreshClick(ActionEvent e) {
        txtName.setText("");
        txtContact.setText("");
        dataModel = BOOKING_BO.getDateModel("", "","","");
        tblHistory.setModel(new DefaultTableModel(dataModel, columnsName));
    }

    private void btnClearClick(ActionEvent e) {
        int confirm = JOptionPane.showConfirmDialog(null, "Are you sure you want to clear the history ?", "Confirm", JOptionPane.YES_NO_OPTION);
        if (confirm == JOptionPane.YES_OPTION) {
            BOOKING_BO.clearHistory();
            dataModel = BOOKING_BO.getDateModel("", "","","");
            tblHistory.setModel(new DefaultTableModel(dataModel, columnsName));
            parentComponent.doSchedule();
        }
    }

    public MainUI getParentComponent() {
        return parentComponent;
    }

    public void setParentComponent(MainUI parentComponent) {
        this.parentComponent = parentComponent;
    }
}
